<template>
  <transition name="showHeader">
    <div v-if="visible" class="header-apps">
      <div v-if="mode === 'sidemenu'" class="header">
        <a-menu style="border-bottom: 0px" mode="horizontal" :default-selected-keys="this.defApp">
          <a-menu-item
            v-for="item in apps"
            :key="item.code"
            style="top: 0px; line-height: 55px; padding-left: 10px; padding-right: 10px"
            @click="switchApp(item.code)"
          >
            {{ item.name }}
          </a-menu-item>
        </a-menu>
      </div>
      <div v-else :class="['top-nav-header-index', theme]"></div>
    </div>
  </transition>
</template>

<script>
import storage from 'store'
import { mixin } from '@/utils/mixin'
import { mapGetters } from 'vuex'
import { APP_MENU } from '@/store/mutation-types'
import { message } from 'ant-design-vue/es'

export default {
  name: 'LeftContent',
  components: {},
  computed: {
    ...mapGetters(['userInfo'])
  },
  created() {
    this.apps = storage.get(APP_MENU)
    this.defApp.push(storage.get(APP_MENU)[0].code)
    console.log('storage.get(APP_MENU)---->', this.apps, this.defApp)
  },
  mixins: [mixin],
  props: {
    mode: {
      type: String,
      // sidemenu, topmenu
      default: 'sidemenu'
    },
    // menus: {
    //   type: Array,
    //   required: true,
    //   default: []
    // },
    theme: {
      type: String,
      required: false,
      default: 'dark'
    },
    collapsed: {
      type: Boolean,
      required: false,
      default: false
    },
    device: {
      type: String,
      required: false,
      default: 'desktop'
    }
  },
  data() {
    return {
      visible: true,
      oldScrollTop: 0,
      defApp: [],
      apps: []
    }
  },
  mounted() {
    document.addEventListener('scroll', this.handleScroll, { passive: true })
  },
  methods: {
    // ...mapActions(['MenuChange']),

    /**
     * 应用切换
     */
    switchApp(appCode) {
      this.defApp = []
      const applicationData = this.userInfo.apps.filter((item) => item.code === appCode)
      const hideMessage = message.loading('正在切换应用！', 0)
      this.MenuChange(applicationData[0])
        .then((res) => {
          hideMessage()
          // eslint-disable-next-line handle-callback-err
        })
        .catch((err) => {
          message.error('应用切换异常')
        })
    },
    handleScroll() {
      if (!this.autoHideHeader) {
        return
      }

      const scrollTop = document.body.scrollTop + document.documentElement.scrollTop
      if (!this.ticking) {
        this.ticking = true
        requestAnimationFrame(() => {
          if (this.oldScrollTop > scrollTop) {
            this.visible = true
          } else if (scrollTop > 300 && this.visible) {
            this.visible = false
          } else if (scrollTop < 300 && !this.visible) {
            this.visible = true
          }
          this.oldScrollTop = scrollTop
          this.ticking = false
        })
      }
    },
    toggle() {
      this.$emit('toggle')
    }
  },
  beforeDestroy() {
    document.body.removeEventListener('scroll', this.handleScroll, true)
  }
}
</script>

<style lang="less">
// @import '../index.less';

.header-apps {
  position: relative;
  z-index: 101; // @ant-global-header-zindex;
}
.showHeader-enter-active {
  transition: all 0.25s ease;
}
.showHeader-leave-active {
  transition: all 0.5s ease;
}
.showHeader-enter,
.showHeader-leave-to {
  opacity: 0;
}
</style>
